package com.dhcc.DRGsHosp.application.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.JdbcTemplateObj;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.KeyDisGroupApplication;
import com.dhcc.DRGsHosp.core.domain.IndexCal;
import com.dhcc.DRGsHosp.core.domain.KeyDisGroup;
import com.dhcc.DRGsHosp.infra.util.DrgsIndex;
import com.dhcc.DRGsHosp.infra.util.StringUtils;

@Named
@Transactional
public class KeyDisGroupApplicationImpl implements KeyDisGroupApplication {
	
	private QueryChannelService queryChannel;
	
	@Autowired
	private JdbcTemplateObj jdbcTemplateObj;
	@Autowired
	private NamedParameterJdbcTemplate jdbcTempSeverice;
	
    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
    private EntityRepository repository;
    
    private EntityRepository getRepository() {
		if (repository == null) {
			repository = InstanceFactory.getInstance(
					EntityRepository.class, "repository");
		}
		return repository;
	}
    
    /**
	 * 
	 * @methodname    :setKeyDisGroup
	 * @description   : 设置重点病组
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月16日
	 */
	@Override
	public int setKeyDisGroup(KeyDisGroup keyDisGroup, String stdid, String verRegion) {
		int keyDisGroupSize = 0;//重点病组数量
		String[] years = keyDisGroup.getYears();//数据年份
		int caseCntPer = keyDisGroup.getCaseCntPer();//病组病例数占比
		int weightPer = keyDisGroup.getWeightPer();//病组权重占比
		int mdcWeightPer = keyDisGroup.getMdcWeightPer();//MDC病组权重占比
		String depCode = keyDisGroup.getDepCode();//科室代码
		String hospCode = keyDisGroup.getHospCode();//科室代码
		String role = keyDisGroup.getRole();//角色
		String user = keyDisGroup.getUser();//用户
		List<String> caseCntList = new  ArrayList<String>();//病例数 病组   
		List<String> weightList = new  ArrayList<String>();//权重 病组
		List<String> mdcWeightList = new  ArrayList<String>();//mdc权重 病组
		StringBuilder sql = new StringBuilder();
		//把比例记录到MR_SIZER_CONDITION表中
		
		String moudleCode = "";
		if(StringUtils.hasLength(depCode)){
			moudleCode = "keyGroup_"+user+"_"+depCode;
		}else{
			moudleCode = "keyGroup_"+user+"_Hosp";
		}
		
		//数组转字符串 用,分割
		String cgfValue = org.apache.commons.lang.StringUtils.join(years, ",")+ "θ" + caseCntPer + "," + weightPer + "," + mdcWeightPer+"θ"+hospCode;//占比
		
		
		sql.append(" declare cnt INTEGER; \n ");
		sql.append(" begin  \n ");
		sql.append(" select count(*) into cnt from drgs.MR_SIZER_CONDITION c where c.role_code = '"+role+"' and c.user_code = '"+user+"' and c.moudle_code = '"+moudleCode+"';   \n ");
		sql.append(" if cnt = 0 then  \n ");
		sql.append(" insert into drgs.MR_SIZER_CONDITION (moudle_code,codition_value,role_code,user_code) values ('"+moudleCode+"','"+cgfValue+"','"+role+"','"+user+"');  \n ");
		sql.append("    else \n ");
		sql.append(" update drgs.MR_SIZER_CONDITION c set c.codition_value = '"+cgfValue+"' where c.moudle_code = '"+moudleCode+"' and c.role_code = '"+role+"' and c.user_code = '"+user+"' ;  \n ");
		sql.append(" end if; \n ");
		sql.append(" commit; \n ");
		sql.append(" end; \n ");
		getRepository().createSqlQuery(sql.toString()).executeUpdate();
		
		
		//计算各病组的病例数
		sql.setLength(0);
		sql.append(" select pardrg_code from( \n ");
		sql.append(" select pardrg_code,(sum(caseCnt) over(order by caseCnt desc))/(sum(caseCnt) over()) perval from  \n ");
		sql.append(" (select pd.pardrg_code,count(mr.pmainindex) caseCnt from drgs.t_patientdrg pd   \n ");
		sql.append(" left join drgs.mr_ipinfo mr on pd.parid = mr.pmainindex and  pd.ver_region='"+verRegion+"'  \n ");
		sql.append(" left join drgs.mr_baseinfo base on mr.pmainindex=base.pmainindex \n ") ;
		sql.append(" where pd.pardrg_code is not null and ( 1<>1  \n ");
		if(null!=years){
			for(int i = 0;i <years.length;i++){
				sql.append(" or to_char(mr.P00290000,'yyyy')='"+years[i]+"' \n ");
			}
		}
		sql.append(" ) \n ");
		if(StringUtils.hasLength(depCode))
		{
			sql.append(" and mr.p00300001 = '"+depCode+"' \n ");
		}
		if(StringUtils.hasLength(hospCode)) {
			sql.append("  and instr('"+hospCode+"',base.p00010001)>0  \n ");
		}
		sql.append(" group by pardrg_code ))where perval <="+(caseCntPer*1.0)/100+" \n ");
		caseCntList = getQueryChannelService().createSqlQuery(sql.toString()).list();
		sql.setLength(0);
		//计算各病组的权重
		sql.append(" select pardrg_code from ( \n ");
		sql.append(" select pardrg_code,(sum(totalWeight) over(order by totalWeight desc))/(sum(totalWeight) over()) perval from(  \n ");
		sql.append(" select pd.pardrg_code,sum(std.as_weight) totalWeight from drgs.t_patientdrg pd   \n ");
		sql.append(" left join drgs.mr_ipinfo mr on pd.parid = mr.pmainindex and  pd.ver_region='"+verRegion+"'  \n ");
		sql.append(" left join drgs.mr_baseinfo base on mr.pmainindex=base.pmainindex \n ") ;
		sql.append(" left join drgs.t_annualstd std on pd.pardrg_code = std.as_drg_code and std.as_id = '"+stdid+"'\n ");
		sql.append(" where pd.pardrg_code is not null and std.as_weight is not null and (1<>1   \n ");
		if(null!=years){
			for(int i = 0;i <years.length;i++){
				sql.append(" or to_char(mr.P00290000,'yyyy')='"+years[i]+"' \n ");
			}
		}
		sql.append(" ) \n ");
		if(StringUtils.hasLength(depCode)){
			sql.append(" and mr.p00300001 = '"+depCode+"' \n ");
		}
		if(StringUtils.hasLength(hospCode)) {
			sql.append("  and instr('"+hospCode+"',base.p00010001)>0  \n ");
		}
		sql.append(" group by pardrg_code ))where perval<= "+(weightPer*1.0)/100+" \n ");
		weightList = getQueryChannelService().createSqlQuery(sql.toString()).list();
		sql.setLength(0);
		//计算各 MDC 下各病组权重 排名
		sql.append(" select pardrg_code from ( \n ");
		sql.append(" select mdc,pardrg_code,(sum(totalWeight) over(order by mdc,totalWeight desc))/(sum(totalWeight) over(order by mdc)) perval  from (    \n ");
		sql.append(" select pd.mdc,pd.pardrg_code,sum(std.as_weight) totalWeight from drgs.t_patientdrg pd   \n ");
		sql.append(" left join drgs.mr_ipinfo mr on pd.parid = mr.pmainindex and  pd.ver_region='"+verRegion+"'  \n ");
		sql.append(" left join drgs.mr_baseinfo base on mr.pmainindex=base.pmainindex \n ") ;
		sql.append(" left join drgs.t_annualstd std on pd.pardrg_code = std.as_drg_code and std.as_id = '"+stdid+"'  \n ");
		sql.append(" where pd.pardrg_code is not null and std.as_weight is not null and(1<>1  \n ");
		if(null!=years){
			for(int i = 0;i <years.length;i++)
			{
				sql.append(" or to_char(mr.P00290000,'yyyy')='"+years[i]+"' \n ");
			}
		}
		sql.append(" )  \n ");
		if(StringUtils.hasLength(depCode)){
			sql.append(" and mr.p00300001 = '"+depCode+"' \n ");
		}
		if(StringUtils.hasLength(hospCode)) {
			sql.append("  and instr('"+hospCode+"',base.p00010001)>0  \n ");
		}
		sql.append(" group by mdc,pardrg_code )) where perval <= "+(mdcWeightPer*1.0)/100+" \n ");
		mdcWeightList = getQueryChannelService().createSqlQuery(sql.toString()).list();
		caseCntList.retainAll(weightList);//求病例数和权重的交集
		caseCntList.retainAll(mdcWeightList);//求病例数和权重的交集后再求MDC权重的交集<<<<<<========重点病组
		if(caseCntList!=null){
			String delSql = "delete drgs.t_keydisgroup t where t.conf_code = '"+moudleCode+"'";
			getRepository().createSqlQuery(delSql).executeUpdate();
			keyDisGroupSize = caseCntList.size();
			//保存新的重点病组数据
			for(int i = 0;i<caseCntList.size();i++){
				addKeyDisGroup(caseCntList.get(i),moudleCode);
			}
		}
		return keyDisGroupSize;
	}
	
	/**
	 * 
	 * @methodname    :getCaseYears
	 * @description   : 获取病例的年份数据
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月16日
	 */
	@Override
	public List<Object> getCaseYears() {
		String caseYearSql = "select distinct(to_char(mr.P00290000,'yyyy')) caseyear from drgs.mr_ipinfo mr  where mr.P00290000 is not null order by caseyear";
		List<Object> totalList = getQueryChannelService().createSqlQuery(caseYearSql).list();
		return totalList;
	}
	@Override
	public List<Object> getCaseYears(String hospCode) {
		String caseYearSql = "select distinct(to_char(mr.P00290000,'yyyy')) caseyear from drgs.mr_ipinfo mr left join  drgs.mr_baseinfo bs on bs.pmainindex=mr.pmainindex  where mr.P00290000 is not null and instr('"+hospCode+"',bs.p00010001)>0 order by caseyear";
		List<Object> totalList = getQueryChannelService().createSqlQuery(caseYearSql).list();
		return totalList;
	}
	/**
	 * 
	 * @methodname    :getKeyDisGroupDataMdc
	 * @description   :获取重点病组数据 按mdc统计
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月15日
	 */
	@Override
	//@Cacheable(value = "drgscom3.1.1", key = "methodName + args[0]")
	public List<Map<String,Object>> getKeyDisGroupDataMdc(String[] years,String depCode,String userCode, String stdId, String verRegion,String hospCode) {
		//查询最新的版本号
		StringBuilder sql = new StringBuilder();
		sql.append("  select a.mdc_code mdcCode ,mdc.mdc_desc mdcDesc,a.drgCnt keyGroupCnt,b.casecnt caseCnt,b.totalweight totalWeight,b.cmi cmi \n ");
		sql.append("  from (select kdg.mdc_code,count(distinct(kdg.drg_code)) drgCnt from drgs.t_keydisgroup kdg \n ");
		if(StringUtils.hasLength(depCode)){
			sql.append("  where kdg.conf_code = 'keyGroup_"+userCode+"_"+depCode+"' \n ");
		}
		else
		{
			sql.append("  where kdg.conf_code = 'keyGroup_"+userCode+"_Hosp' \n ");
		}
		sql.append("  group by kdg.mdc_code ) a \n ");
		sql.append("  left join (select pd.mdc,pd.ver_code,count(*) caseCnt,sum(std.as_weight)  totalWeight,round(avg(std.as_weight),2) cmi   \n ");
		sql.append("  from drgs.t_keydisgroup kdg  \n ");
		sql.append("  left join drgs.t_patientdrg pd on kdg.drg_code = pd.pardrg_code and  pd.ver_region='"+verRegion+"' \n ");
		if(StringUtils.hasLength(depCode))
		{
			sql.append("  and kdg.conf_code = 'keyGroup_"+userCode+"_"+depCode+"' \n ");
		}
		else
		{
			sql.append("  and kdg.conf_code = 'keyGroup_"+userCode+"_Hosp' \n ");
		}
		sql.append("  left join drgs.mr_baseinfo a on pd.parid = a.pmainindex \n ");
		sql.append("  left join drgs.mr_ipinfo mr on pd.parid = mr.pmainindex \n ");
		sql.append("  left join drgs.t_annualstd std on std.as_drg_code = pd.pardrg_code and std.as_id = '"+stdId+"' \n ");
		sql.append("  where 1=1  and ( to_char(mr.P00290000,'yyyy')= '' \n ");
		for(int i = 0;null!=years&&i<years.length;i++)
		{
			sql.append(" or to_char(mr.P00290000,'yyyy')= '"+years[i]+"' \n ");
		}
		sql.append("  ) and pd.pardrg_code is not null  \n ");
		if(StringUtils.hasLength(depCode))
		{
			sql.append("  and mr.p00300001 = '"+depCode+"' \n ");
		}
		if(StringUtils.hasLength(hospCode)) {
			sql.append("  and instr('"+hospCode+"',a.p00010001)>0  \n ");
		}
		sql.append("  group by pd.mdc,pd.ver_code) b  on a.mdc_code = b.mdc \n ");
		sql.append("  left join drgs.t_mdcgroup mdc on a.mdc_code = mdc.mdc_code and mdc.ver_code = b.ver_code \n ");
		List<Map<String,Object>> result = jdbcTemplateObj.queryForList(sql.toString());
		return result;
	}
	
	/**
	 * 
	 * @methodname    :getKeyDisGroupDataDrg
	 * @description   : 获取重点病组数据 按drg统计  MDC下DRG或者全部MDC下DRG
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月16日
	 */
	@Override
//	@Cacheable(value = "drgscom3.1.1", key = "methodName + args[0] + args[1]")
	public List<Map<String,Object>> getKeyDisGroupDataDrg(KeyDisGroup keyDisGroup, String stdid, String verRegion) {
		//查询最新的版本号
		String mdc =keyDisGroup.getMdcCode();
		String[] years = keyDisGroup.getYears();
		String depCode = keyDisGroup.getDepCode();
		String user =keyDisGroup.getUser();//用户code
		String hospCode =keyDisGroup.getHospCode();//用户code
		StringBuilder sql = new StringBuilder();
		sql.append(" select mdc.mdc_code mdcCode,mdc.mdc_desc mdcDesc,a.drg_code drgCode,drg.drg_desc drgDesc,casecnt caseCnt,casecntrow caseCntRange,as_weight weight,totalweight totalWeight,totalweightrow totalWeightRange \n ");
		sql.append(" from (select drg_code,ver_code,casecnt,rownum casecntrow,totalweight,totalweightrow,as_weight  from  \n ");
		sql.append(" (select drg_code,ver_code,casecnt,totalweight,rownum totalweightrow,as_weight from  \n ");
		sql.append(" (select a.drg_code,ver_code,b.as_weight, b.casecnt,b.totalweight  \n ");
		sql.append(" from (select kdg.drg_code from drgs.t_keydisgroup kdg where 1=1 \n ");
		
		if(StringUtils.hasLength(mdc))
		{
			sql.append(" and kdg.mdc_code='"+mdc+"' \n ");
		}
		if(StringUtils.hasLength(depCode))
		{
			sql.append(" and kdg.conf_code='keyGroup_"+user+"_"+depCode+"' \n ");
		}
		else
		{
			sql.append(" and kdg.conf_code='keyGroup_"+user+"_Hosp' \n ");
		}
		sql.append("  ) a left join ( \n ");
		sql.append(" select pd.pardrg_code,pd.ver_code,count(*) caseCnt,avg(std.as_weight) as_weight,sum(std.as_weight)  totalWeight   \n ");
		sql.append(" from drgs.t_keydisgroup kdg   \n ");
		sql.append(" left join drgs.t_patientdrg pd on kdg.drg_code = pd.pardrg_code  and  pd.ver_region='"+verRegion+"' \n ");
		if(StringUtils.hasLength(depCode))
		{
			sql.append(" and kdg.conf_code='keyGroup_"+user+"_"+depCode+"' \n ");
		}
		else
		{
			sql.append(" and kdg.conf_code='keyGroup_"+user+"_Hosp' \n ");
		}
		sql.append("  left join drgs.mr_baseinfo a on pd.parid = a.pmainindex \n ");
		sql.append(" left join drgs.mr_ipinfo mr on pd.parid = mr.pmainindex  \n ");
		sql.append(" left join drgs.t_annualstd std on std.as_drg_code = pd.pardrg_code and std.as_id = '"+stdid+"' \n ");
		sql.append(" where 1=1 \n ");
		if(StringUtils.hasLength(mdc))
		{
			sql.append(" and kdg.mdc_code='"+mdc+"' \n ");
		}
		
		sql.append(" and ( to_char(mr.P00290000,'yyyy')= '' \n ");
		for(int i = 0;null!=years&&i<years.length;i++)
		{
			sql.append(" or to_char(mr.P00290000,'yyyy')= '"+years[i]+"' \n ");
		}
		sql.append(" ) and pd.pardrg_code is not null \n ");
		if(StringUtils.hasLength(depCode))
		{
			sql.append(" and mr.p00300001 = '"+depCode+"' \n ");
		}
		if(StringUtils.hasLength(hospCode)){
			sql.append("  and instr('"+hospCode+"',a.p00010001)>0  \n ");
		}
		sql.append(" group by pd.pardrg_code,pd.ver_code    \n ");
		sql.append(" ) b  on a.drg_code = b.pardrg_code order by totalweight desc nulls last ) order by casecnt desc nulls last))a \n ");
		sql.append(" left join drgs.t_drgs drg on drg.drg_code = a.drg_code and drg.ver_code = a.ver_code \n ");
		sql.append(" join drgs.t_adrg adrg on drg.adrg_code = adrg.adrg_code and drg.ver_code = adrg.ver_code  \n ");
		sql.append(" join drgs.t_mdcgroup mdc on mdc.mdc_code = adrg.mdc_code and mdc.ver_code = adrg.ver_code  \n ");
		sql.append(" order by casecntrow  \n ");
		List<Map<String,Object>> result = jdbcTemplateObj.queryForList(sql.toString());
		return result;
	}
	
	/**
	 * 
	 * @methodname    :getExtraDisGroup
	 * @description   : 获取重点病组以外的病组 让用户筛选
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月17日
	 */
	@Override
//	@Cacheable(value = "drgscom3.1.1", key = "methodName + args[0]")
	public List<Map<String,Object>> getExtraDisGroup(String[] years,String depCode,String userCode,String stdId, String verRegion,String hospCode) {
		//查询最新的版本号
		StringBuilder sql = new StringBuilder();
		sql.append("  select  mdc.mdc_code mdcCode,mdc.mdc_desc mdcDesc,pardrg_code drgCode,drg.drg_desc drgDesc,casecnt,casecntrow caseCntRange,as_weight weight,totalweight,totalweightrow  totalWeightRange from (  \n ");
		sql.append("  select pardrg_code,ver_code,caseCnt,as_weight,totalWeight,totalWeightRow,rownum caseCntRow from (  \n ");
		sql.append("  select pardrg_code,ver_code,caseCnt,as_weight,totalWeight,rownum totalWeightRow from  \n ");
		sql.append("  (select pardrg_code,ver_code,caseCnt,as_weight,totalWeight from (   \n ");
		sql.append("  select pd.pardrg_code,pd.ver_code,count(pd.parid) caseCnt,avg(std.as_weight) as_weight,sum(std.as_weight)  totalWeight    \n ");
		sql.append("  from drgs.t_patientdrg pd \n ");
		sql.append("  left join drgs.mr_baseinfo a on pd.parid = a.pmainindex \n ");
		sql.append("  left join drgs.mr_ipinfo mr on pd.parid = mr.pmainindex  and  pd.ver_region='"+verRegion+"'   \n ") ;
		sql.append("  left join drgs.t_annualstd std on std.as_drg_code = pd.pardrg_code and std.as_id = '"+stdId+"'\n ");
		sql.append("  where 1=1  \n ");
		if(StringUtils.hasLength(depCode))
		{
			sql.append("  and mr.p00300001 = '"+depCode+"' \n ");
		}
		
		sql.append(" and not exists (select kdg.drg_code from drgs.t_keydisgroup kdg where kdg.drg_code = pd.pardrg_code \n ");
		if(StringUtils.hasLength(depCode))
		{
			sql.append("  and kdg.conf_code = 'keyGroup_"+userCode+"_"+depCode+"' \n ");
		}
		else
		{
			sql.append("  and kdg.conf_code = 'keyGroup_"+userCode+"_Hosp' \n ");
		}
		if(StringUtils.hasLength(hospCode)){
			sql.append("  and instr('"+hospCode+"',a.p00010001)>0  \n ");
		}
		sql.append(" ) \n ");
		sql.append("  and ( 1<>1  \n ");
		for(int i = 0;null!=years&&i<years.length;i++)
		{
			sql.append(" or to_char(mr.P00290000,'yyyy')= '"+years[i]+"' \n ");
		}
		sql.append("  ) and pd.pardrg_code is not null group by pd.pardrg_code,pd.ver_code) a  \n ");
		sql.append("  order by totalWeight desc nulls last) order by caseCnt desc)) a \n ");
		sql.append("  left join drgs.t_drgs drg on drg.drg_code = a.pardrg_code and drg.ver_code = a.ver_code \n ");
		sql.append("  join drgs.t_adrg adrg on drg.adrg_code = adrg.adrg_code and drg.ver_code = adrg.ver_code \n ");
		sql.append("  join drgs.t_mdcgroup mdc on mdc.mdc_code = adrg.mdc_code and mdc.ver_code = adrg.ver_code \n ");
		sql.append("  order by casecntrow \n ");
		List<Map<String,Object>> result =   jdbcTemplateObj.queryForList(sql.toString()) ;
		return result;
	}
	
	
	/**
	 * 
	 * @methodname    :delKeyDisGroup
	 * @description   : 删除重点病组
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月16日
	 */
	@Override
	public void delKeyDisGroup(String drgCode,String depCode,String userCode) {
		String deleteSql = "delete drgs.t_keydisgroup kdg where kdg.drg_code = :drgCode and kdg.conf_code = :confCode";
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("drgCode",drgCode);
		if(StringUtils.hasLength(depCode))
		{
			param.put("confCode","keyGroup_"+userCode+"_"+depCode);
		}
		else {
			param.put("confCode","keyGroup_"+userCode+"_Hosp");
		}
		jdbcTempSeverice.update(deleteSql,param);
	}
	
	/**
	 * 
	 * @methodname    :addKeyDisGroup
	 * @description   : 添加重点病组
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月17日
	 */
	@Override
	public void addKeyDisGroup(String drgCode,String confCode) {
		KeyDisGroup k = new KeyDisGroup();
		String mdc = drgCode.charAt(0)+"";
		String type = drgCode.charAt(1)+"";
		switch(type)
		{
		case "A":
		case "B":
		case "C":
		case "D":
		case "E":
		case "F":
		case "G":
		case "H":
		case "J":
			type = "S";//外科
			break;
		case "K":
		case "L":
		case "M":
		case "N":
		case "P":
		case "Q":
			type = "O";//操作
			break;
		case "R":
		case "S":
		case "T":
		case "U":
		case "V":
		case "W":
		case "X":
		case "Y":
		case "Z":
			type = "I";//内科
			break;
		}
		
		k.setDrgCode(drgCode);
		k.setMdcCode("MDC"+mdc);
		k.setGroupType(type);
		k.setConfCode(confCode);
		k.save();
	}//更改SQL查询时间
	
	/**
	 * 
	 * @methodname    :getDisGroupCollectInfo
	 * @description   : 获取病组的汇总信息
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月19日
	 */
	@Override
//	@Cacheable(value = "drgscom3.1.1", key = "'getDisGroupCollectInfo' + #dateFrom + #dateTo")
	public List<Map<String,Object>> getDisGroupCollectInfo(IndexCal index) {
		StringBuilder sql = new StringBuilder();
		String dateFrom =DrgsIndex.formateDateFrom(index.getDateFrom());
		String dateTo = DrgsIndex.formateDateTo(index.getDateTo());
		String depCode = index.getPardepCode();
		String stdId = index.getStdId();
		String verRegion=index.getVerRegion();
		String userCode = index.getUserCode();
		String hospCode = index.getHospCode();
		sql.append(" select count(distinct(kdg.drg_code)) keyGroupCnt,count(distinct(pd.pardrg_code)) groupCnt, \n ");
		sql.append(" count(kdg.drg_code) keyCaseCnt,count(pd.pardrg_code) caseCnt, \n ");
		sql.append(" sum(case when kdg.drg_code is null then 0 else std.as_weight end) keyTotalWeight, \n ");
		sql.append(" sum(std.as_weight) totalWeight \n ");
		if(!StringUtils.hasLength(depCode)){//全院重点病组
			sql.append(" ,count(distinct(case when kdg.group_type='I' then kdg.drg_code  else null end)) internalCnt, \n ");
			sql.append(" count(distinct(case when kdg.group_type='S' then kdg.drg_code  else null end)) surgeryCnt, \n ");
			sql.append(" count(distinct(case when kdg.group_type='O' then kdg.drg_code  else null end)) operationCnt, \n ");
			sql.append(" sum(case when kdg.drg_code is not null and o.PO0301 = '1'  then 1 else 0 end) level1, \n ");
			sql.append(" sum(case when kdg.drg_code is not null and o.PO0301 = '2'  then 1 else 0 end) level2, \n ");
			sql.append(" sum(case when kdg.drg_code is not null and o.PO0301 = '3'  then 1 else 0 end) level3, \n ");
			sql.append(" sum(case when kdg.drg_code is not null and o.PO0301 = '4'  then 1 else 0 end) level4 \n ");
		}
		sql.append(" from drgs.t_patientdrg pd  \n ");
		sql.append(" left join drgs.mr_baseinfo a on pd.parid = a.pmainindex \n ");
		sql.append(" left join drgs.mr_ipinfo mr on pd.parid = mr.pmainindex  and  pd.ver_region='"+verRegion+"'\n ");
		sql.append(" left join drgs.t_annualstd std on std.as_drg_code = pd.pardrg_code and std.as_id ='"+stdId+"' \n ");
		sql.append(" left join drgs.t_keydisgroup kdg on kdg.drg_code = pd.pardrg_code \n ");
		if(StringUtils.hasLength(depCode)){//科室重点病组
			sql.append(" and kdg.conf_code = 'keyGroup_"+userCode+"_"+depCode+"' \n ");
		}else{
			sql.append(" and kdg.conf_code = 'keyGroup_"+userCode+"_Hosp' \n ");
		}
		if(!StringUtils.hasLength(depCode)){//全院重点病组
			sql.append(" left join drgs.mr_operation o on o.pmainindex = mr.pmainindex and o.POTYPE = 'M' and o.PO1400='1' \n ");
		}
		sql.append(" where 1=1 and to_char(mr.P00290000,'yyyy-mm-dd') between '"+dateFrom+"' and '"+dateTo+"'and pd.pardrg_code is not null  \n ");
		if(StringUtils.hasLength(depCode)){//科室重点病组
			sql.append(" and mr.p00300001 = '"+depCode+"'  \n ");
		}
		if(StringUtils.hasLength(hospCode)){
			sql.append("  and instr('"+hospCode+"',a.p00010001)>0  \n ");
		}
		List<Map<String,Object>> result = jdbcTemplateObj.queryForList(sql.toString());
		//重点病组个数占比
		return result;
	}
	
	/**
	 * 
	 * @methodname    :getCaseFeeDistribute
	 * @description   : 获取病例费用分布数据
	 * @param         :
	 * @author        :mazhaoyan
	 * @return        :
	 * @date          :2016年12月23日
	 */
	@Override
	@Cacheable(value = "drgscom3.1.1", key = "'getCaseFeeDistribute' + #indexCal.dateFrom + #indexCal.dateTo "
			+ "+ #indexCal.pardrgCode + #indexCal.compareType + #indexCal.pType + #indexCal.msCode "
			+ "+ #indexCal.stdId + #indexCal.manageYear +#indexCal.verRegion + #sliderValue")
	public List<Map<String,Object>> getCaseFeeDistribute(IndexCal indexCal,int sliderValue){	
		String dateFrom = indexCal.getDateFrom();//开始时间
		String dateTo = indexCal.getDateTo();//结束时间
		String drgCode = indexCal.getPardrgCode();//drg代码
		String compareType = indexCal.getCompareType();//比较类型
		String pType = indexCal.getpType();//病人类型
		String outCode = indexCal.getMsCode();//外部代码
		String manageYear = indexCal.getManageYear();
		String stdId = indexCal.getStdId();
		String verRegion=indexCal.getVerRegion();
		String depCode = indexCal.getPardepCode();//科室代码
		StringBuilder sql = new StringBuilder();
		String hospCode = indexCal.getHospCode();
		sql.append(" SELECT LV FEEINTERVAL,COUNT(FEEINTERVAL) INCNT,ROUND(AVG(P00800000),2) AVGFEE,ROUND(AVG(IN_DAYS),2) AVGDAY,   \n ");
		sql.append(" SUM(AS_WEIGHT) TOTALWEIGHT,AVG(FEE) STDFEE,ROUND(SUM(DRUGCOST)/SUM(P00800000)*100,2)    \n ");
		sql.append(" DRUGRATIO FROM (SELECT LEVEL LV FROM DUAL CONNECT BY LEVEL <=4) LV LEFT JOIN (    \n ");
		sql.append(" SELECT CASE WHEN MR.P00800000 >= TARGET.FEE*(100+"+sliderValue+")/100 THEN 1 WHEN   \n ");
		sql.append(" MR.P00800000 > TARGET.FEE AND MR.P00800000 < TARGET.FEE*(100+"+sliderValue+")/100 THEN 2   \n ");
		sql.append(" WHEN MR.P00800000 > (100-"+sliderValue+")/100*TARGET.FEE AND MR.P00800000 <= TARGET.FEE THEN 3   \n ");
		sql.append(" WHEN MR.P00800000 <= (100-"+sliderValue+")/100*TARGET.FEE THEN 4 END FEEINTERVAL,  \n ");
		sql.append(" MR.P00800000,MR.P00320000 IN_DAYS,STD.AS_WEIGHT,EXPSUM.DRUG_COST DRUGCOST,TARGET.FEE   \n ");
		sql.append(" FROM DRGS.T_PATIENTDRG PD \n ");
		sql.append(" LEFT JOIN DRGS.MR_BASEINFO A ON A.PMAININDEX = PD.PARID \n ");
		sql.append(" LEFT JOIN DRGS.MR_IPINFO MR ON PD.PARID = MR.PMAININDEX  and pd.ver_region='"+verRegion+"'   \n ");
		sql.append(" LEFT JOIN DRGS.MR_EXPENSE_SUM EXPSUM ON MR.PMAININDEX = EXPSUM.PMAININDEX  \n ");
		sql.append(" LEFT JOIN DRGS.T_ANNUALSTD STD ON PD.PARDRG_CODE = STD.AS_DRG_CODE  AND STD.AS_ID ='"+stdId+"'\n ");
		if("manage".equals(compareType)){
			sql.append(" LEFT JOIN(SELECT PARDRG_CODE, MANAGE_BENCHMARK FEE \n ");
			sql.append(" FROM DRGS.T_QUOTAMANAGE \n ");
			sql.append(" WHERE PARDEP_CODE IS NULL \n ");
			sql.append(" AND PARDEPGROUP_CODE IS NULL \n ");
			sql.append(" AND PARQUOTA_CODE='AvgFee' \n ");
//			sql.append(" AND PATIENT_TYPE='"+pType+"' \n ");
			sql.append(" AND PAR_MS_CODE='"+outCode+"' \n ");
			sql.append(" AND MANAGE_YEAR ='"+manageYear+"' \n ");
			sql.append(" ) TARGET ON TARGET.PARDRG_CODE = PD.PARDRG_CODE  \n ");
		}else if("std".equals(compareType)){
			sql.append(" LEFT JOIN(SELECT X.PARDRG_CODE, X.MANAGE_BENCHMARK  FEE  \n ");
			sql.append(" FROM DRGS.T_MANAGESUMMARYDTL X  \n "); 
			sql.append(" WHERE X.PARDRG_CODE IS NOT NULL  \n "); 
			sql.append(" AND X.PARQUOTA_CODE='AvgFee'  \n "); 
			sql.append(" AND X.STD_YEAR='"+manageYear+"'  \n "); 
//			sql.append(" AND X.PATIENT_TYPE='"+pType+"'  \n ");
			sql.append(" AND X.PAR_MS_CODE='"+outCode+"'  \n ");
			sql.append(" ) TARGET ON TARGET.PARDRG_CODE = PD.PARDRG_CODE  \n "); 
		}else {
			sql.append(" LEFT JOIN(SELECT B.PARDRG_CODE, AVG(A.P00800000) FEE  \n "); 
			sql.append(" FROM  DRGS.MR_IPINFO A  \n ");  
			sql.append(" LEFT JOIN DRGS.T_PATIENTDRG B     ON A.PMAININDEX=B.PARID  and  b.ver_region='"+verRegion+"'  \n ");
			if("1".equals(pType)){
				sql.append(" LEFT JOIN DRGS.T_MEDINS D ON D.PARID=A.PMAININDEX  \n ");
			}
			sql.append(" WHERE B.PARDRG_CODE IS NOT NULL  \n "); 
			sql.append(" AND TO_CHAR(A.P00290000,'YYYY-MM-DD') BETWEEN  '"+dateFrom+"'  AND '"+dateTo+"'  \n ");
			sql.append(" GROUP BY B.PARDRG_CODE  \n ");
			sql.append(" ) TARGET ON TARGET.PARDRG_CODE = PD.PARDRG_CODE  \n "); 
		}
		sql.append(" WHERE TO_CHAR(MR.P00290000,'YYYY-MM-DD')  BETWEEN '"+dateFrom+"' AND '"+dateTo+"'   \n ");
		if(StringUtils.hasLength(depCode)){
			sql.append(" AND MR.P00300001 = '"+depCode+"' \n ");
		}
		if(StringUtils.hasLength(hospCode)){
			sql.append("  and instr('"+hospCode+"',a.p00010001)>0  \n ");
		}
		sql.append(" AND PD.PARDRG_CODE = '"+drgCode+"') T  \n ");
		sql.append(" ON LV.LV = T.FEEINTERVAL  GROUP BY LV ORDER BY LV \n ");
		List<Map<String,Object>> result = jdbcTemplateObj.queryForList(sql.toString());
		//病例费用分布
		return result;
	}

}
